package com.ruoyi.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.graduate.domain.entity.MsPhotographyOrder;
import com.ruoyi.graduate.domain.entity.MsSysUser;
import com.ruoyi.graduate.domain.param.*;
import com.ruoyi.graduate.domain.vo.MsPhotoUserListVo;
import com.ruoyi.graduate.domain.vo.MsSysUserPageVo;
import com.ruoyi.graduate.mapper.MsPhotographyOrderMapper;
import com.ruoyi.graduate.mapper.MsSysUserMapper;
import com.ruoyi.graduate.mapping.MsSysUserMapping;
import com.ruoyi.graduate.service.IMsSysUserService;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class IMsSysUserServiceImpl extends ServiceImpl<MsSysUserMapper, MsSysUser>
implements IMsSysUserService {

    @Autowired
    private MsSysUserMapper msSysUserMapper;

    @Autowired
    private MsPhotographyOrderMapper msPhotographyOrderMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;


    @Override
    public PageInfo pageList(MsSysUserPageParam param) {

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MsSysUserPageVo> msSysUserPageVos = msSysUserMapper.pageList(param.getNickName());

        return new PageInfo(msSysUserPageVos);
    }

    @Override
    public List<MsPhotoUserListVo> photoList() {
        return msSysUserMapper.photoList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updata(MsSysUserUpdataParam param) {

        MsSysUser updata = MsSysUserMapping.INSTANCE.updata(param);
        int i = msSysUserMapper.updateById(updata);

        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delete(Long id) {

        QueryWrapper qw1 = new QueryWrapper();
        qw1.eq("customer_id", id);
        qw1.in("order_status", 1,2,3);

        List list1 = msSysUserMapper.selectList(qw1);

        if (list1.size() > 0) {
            return AjaxResult.error("该用户还有未结束订单不能删除");
        }

        QueryWrapper qw2 = new QueryWrapper();
        qw2.eq("photographer_id", id);
        qw2.in("order_status", 1,2,3);

        List list2 = msSysUserMapper.selectList(qw2);

        if (list2.size() > 0) {
            return AjaxResult.error("该摄影师还有未完成订单不能删除");
        }

        int i = msSysUserMapper.deleteById(id);

        return i > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult reset(MsSysUserResetParam param) {

        UpdateWrapper uw = new UpdateWrapper();
        uw.eq("user_id", param.getUserId());
        uw.set("password", SecurityUtils.encryptPassword(param.getPassword()));

        int update = msSysUserMapper.update(null, uw);

        return update > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult adduser(MsSysUserAddParam param) {

        MsSysUser sysUser = new MsSysUser();
        sysUser.setUserName(param.getUsername());
        sysUser.setPassword(SecurityUtils.encryptPassword(param.getPassword()));
        if (!StringUtils.isEmpty(param.getPhonenumber())) {
            sysUser.setPhonenumber(param.getPhonenumber());
        }

        msSysUserMapper.insert(sysUser);

        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setRoleId(param.getRole());
        sysUserRole.setUserId(sysUser.getUserId());
        int insert = sysUserRoleMapper.insert(sysUserRole);

        return insert > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult resetPassword(MsSysUserRestPasswordParam param) {

        MsSysUser msSysUser = msSysUserMapper.selectById(param.getUserId());

        if (SecurityUtils.matchesPassword(param.getPassword(),msSysUser.getPassword())){

            msSysUser.setPassword(SecurityUtils.encryptPassword(param.getNewPassword()));

            int i = msSysUserMapper.updateById(msSysUser);

            return i > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
        }

        return AjaxResult.error("密码错误请重新输入！");
    }
}
